package net.wanho.ems.controller;

import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import net.wanho.ems.entity.User;
import net.wanho.ems.service.UserService;
import net.wanho.ems.vo.AjaxResult;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Author：汤小洋
 * Date：2023-07-06 16:29
 * Description：<描述>
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @RequestMapping("/register")
    public AjaxResult register(User user){
        userService.register(user);
        return AjaxResult.success();
    }

    @RequestMapping("/login")
    public AjaxResult login(User user){
        User u = userService.login(user);
        return AjaxResult.success(u);
    }

    @RequestMapping("/modifyPassword")
    public AjaxResult modifyPassword(Integer id,String oldpassword,String newpassword){
        userService.modifyPassword(id,oldpassword,newpassword);
        return AjaxResult.success();
    }

    @RequestMapping("/list")
    public AjaxResult list(Integer page,Integer limit,User user){
        PageInfo<User> pageInfo = userService.list(page,limit,user);
        return AjaxResult.success(pageInfo.getList(),pageInfo.getTotal());
    }

    @RequestMapping("/modifyStatus")
    public AjaxResult modifyStatus(Integer id,Integer status){
        userService.modifyStatus(id,status);
        return AjaxResult.success();
    }

    @RequestMapping("/removeById")
    public AjaxResult removeById(Integer id){
        userService.removeById(id);
        return AjaxResult.success();
    }

    @RequestMapping("/findById")
    public  AjaxResult findById(Integer id){
        return AjaxResult.success(userService.findById(id));
    }

    @RequestMapping("/modify")
    public AjaxResult modify(User user){
        userService.modify(user);
        return AjaxResult.success();
    }

    @RequestMapping("/resetPassword")
    public AjaxResult resetPassword(Integer id){
        userService.resetPassword(id);
        return AjaxResult.success();
    }

}
